#include "../../mqserver/exchange.hpp"

#include <iostream>
#include <gtest/gtest.h>

NS_Exchange::ExchangeManager emp("./meta/test");

class ExchangeTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
    }

    virtual void TearDown() override
    {
    }
};

// 测试交换机管理器的插入，查找，删除

// TEST(exchange_test, insert_test)
// {
//     std::string str_args = "k1=v1&k2=v2";
//     emp.declareExchange("exchange1", NS_Message::ExchangeType::DIRECT, true, true, str_args);
//     emp.declareExchange("exchange2", NS_Message::ExchangeType::DIRECT, true, true, str_args);
//     emp.declareExchange("exchange3", NS_Message::ExchangeType::DIRECT, true, true, str_args);
//     emp.declareExchange("exchange4", NS_Message::ExchangeType::DIRECT, true, true, str_args);
//     ASSERT_EQ(emp.size(), 4);
// }

// TEST(exchange_test, delete_test)
// {
//     emp.deleteExchange("exchange3");
//     ASSERT_EQ(emp.size(), 3);
// }

// TEST(exchange_test, select_test)
// {
//     NS_Exchange::Exchange::Ptr e3 =  emp.selectOne("exchange3");
//     ASSERT_EQ(e3, nullptr);

//     NS_Exchange::Exchange::Ptr e2 =  emp.selectOne("exchange2");
//     ASSERT_NE(e2, nullptr);
//     ASSERT_EQ(e2->name, std::string("exchange2"));
//     ASSERT_EQ(e2->type, NS_Message::ExchangeType::DIRECT);
//     ASSERT_EQ(e2->durable, true);
//     ASSERT_EQ(e2->auto_delete, true);
// }

// 测试交换机类型的其他参数为空时的错误检测 exchange.hpp:173 190
// TEST(exchange_test, select_over_test)
// {
//     emp.declareExchange("exchange1", NS_Message::ExchangeType::DIRECT, true, true, "");
//     NS_Exchange::Exchange::Ptr e1 = emp.selectOne("exchange1");
//     ASSERT_NE(e1, nullptr);
// }

// 测试交换机持久化类的插入、查找
// NS_Exchange::ExchangePersist per("./meta/test");

// class ExchangeTest : public testing::Environment
// {
// public:
//     virtual void SetUp() override
//     {
//     }

//     virtual void TearDown() override
//     {
//     }
// };

// TEST(exchange_test, insert_test)
// {
//     per.createTable();
//     std::string str_args = "k1=v1&k2=v2";
//     NS_Exchange::Exchange::Ptr ptr = std::make_shared<NS_Exchange::Exchange>("exchange1", NS_Message::ExchangeType::DIRECT, true, true);
//     ptr->setArgs(str_args);
//     per.insert(ptr);
// }

// TEST(exchange_test, select_test)
// {
//     NS_Exchange::Exchange::Ptr ptr = std::make_shared<NS_Exchange::Exchange>();
//     ptr = per.selectOne("exchange1");
//     ASSERT_EQ(ptr->name, std::string("exchange1"));
// }

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ExchangeTest());
    RUN_ALL_TESTS();
    return 0;
}